<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<meta charset="utf-8">
	<title>用户管理管理</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta http-equiv="Access-Control-Allow-Origin" content="*">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="icon" href="favicon.ico">
	<link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="/resources/css/public.css" media="all" />
	<link rel="stylesheet" href="/resources/layui_ext/dtree/dtree.css">
	<link rel="stylesheet" href="/resources/layui_ext/dtree/font/dtreefont.css">
	
	<style type="text/css">
		.select-test{position: absolute;max-height: 500px;height: 350px;overflow: auto;width: 100%;z-index: 123;display: none;border:1px solid silver;top: 42px;}
		.layui-show{display: block!important;}
	</style>
</head>
<body class="childrenBody">
	<!-- 搜索条件开始 -->
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
	  <legend>查询条件</legend>
	</fieldset>
	<form class="layui-form" method="post" id="searchFrm">
		<div class="layui-form-item">
		    <div class="layui-inline">
		      <label class="layui-form-label">用户姓名:</label>
		      <div class="layui-input-inline">
		        <input type="text" name="name"  autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-inline">
		      <label class="layui-form-label">用户地址:</label>
		      <div class="layui-input-inline">
		        <input type="text" name="address"  autocomplete="off" class="layui-input">
		      </div>
		    </div>
		    <div class="layui-inline">
		      <button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" id="doSearch">查询</button>
		      <button type="reset" class="layui-btn layui-btn-warm  layui-icon layui-icon-refresh">重置</button>
		    </div>
		 </div>
	</form>
	
	<!-- 搜索条件结束 -->
	
	
	<!-- 数据表格开始 -->
	<table class="layui-hide" id="userTable" lay-filter="userTable"></table>
	<div style="display: none;" id="userToolBar">
		    <button type="button" class="layui-btn layui-btn-sm" lay-event="add" shiro:hasPermission="user:create">增加</button>
	</div>
	<div  id="userBar" style="display: none;">
	  <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="user:update">编辑</a>
	  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="user:delete">删除</a>
	  <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="user:resetpwd">重置密码</a>
	  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="selectUserRole" shiro:hasPermission="user:selectRole">分配角色</a>
	</div>
	<!-- 数据表格结束 -->
	
	<!-- 添加和修改的弹出层开始 -->
	<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
		<form class="layui-form"  lay-filter="dataFrm" id="dataFrm">
			<div class="layui-form-item">
				<div class="layui-inline">
				  <label class="layui-form-label">所在部门:</label>
				   <div class="layui-input-inline">
				      <div class="layui-unselect layui-form-select" id="pid_div">
				        <div class="layui-select-title">
				          <input type="hidden" name="deptid"  id="pid">
				          <input type="text" name="pid_str" id="pid_str" placeholder="请选择" readonly="" class="layui-input layui-unselect">
				          <i class="layui-edge"></i>
				        </div>
				      </div>
				      <div class="layui-card select-test" id="deptSelectDiv">
				        <div class="layui-card-body">
				          <div id="toolbarDiv"><ul id="deptTree" class="dtree" data-id="0" style="width: 100%;"></ul></div>
				        </div>
				      </div>
				  </div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">排序码:</label>
					<div class="layui-input-inline">
						<input type="hidden" name="id">
						<input type="text" name="ordernum" id="ordernum"   lay-verify="required"  placeholder="请输入用户排序码" autocomplete="off"
							class="layui-input">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline">
				  <label class="layui-form-label">领导部门:</label>
				   <div class="layui-input-inline">
				      <div class="layui-unselect layui-form-select" id="leaderpid_div">
				        <div class="layui-select-title">
				          <input type="text" name="leaderpid_str" id="leaderpid_str" placeholder="请选择" readonly="" class="layui-input layui-unselect">
				          <i class="layui-edge"></i>
				        </div>
				      </div>
				      <div class="layui-card select-test" id="leaderDeptSelectDiv">
				        <div class="layui-card-body">
				          <div id="toolbarDiv"><ul id="leaderDeptTree" class="dtree" data-id="0" style="width: 100%;"></ul></div>
				        </div>
				      </div>
				  </div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">领导姓名:</label>
					<div class="layui-input-inline">
						<select id="mgr" name="mgr">
							<option value="null">请选择领导</option>
						</select>
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">用户名称:</label>
					<div class="layui-input-inline">
						<input type="text" name="name" id="username"  lay-verify="required"  placeholder="请输入用户名称" autocomplete="off"
							class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">登陆名称:</label>
					<div class="layui-input-block">
						<input type="text" name="loginname" id="loginname"  lay-verify="require" placeholder="请输入用户登陆名" autocomplete="off"
							class="layui-input">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">用户地址:</label>
						<div class="layui-input-block">
							<input type="text" name="address" placeholder="请输入用户地址" autocomplete="off"
								class="layui-input">
						</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">用户备注:</label>
				<div class="layui-input-block">
					<textarea  name="remark"   autocomplete="off"
						class="layui-textarea"> </textarea>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">用户性别:</label>
					<div class="layui-input-inline">
						 <input type="radio" name="sex" value="1" title="男" checked="checked">
						 <input type="radio" name="sex" value="0" title="女"  >
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">是否可用:</label>
					<div class="layui-input-inline">
						 <input type="radio" name="available" value="1" checked="checked" title="可用">
						 <input type="radio" name="available" value="0" title="不可">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">入职时间:</label>
						<div class="layui-input-block">
							<input type="text" name="hiredate" id="hiredate" placeholder="请选择入职时间" readonly="readonly" autocomplete="off"
								class="layui-input">
						</div>
			</div>
			<div class="layui-form-item" style="text-align: center;">
			    <div class="layui-input-block">
			      <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
			      <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
			    </div>
		  	</div>
		</form>
	
	</div>
	<!-- 添加和修改的弹出层结束 -->
	
	<!-- 分配角色弹出层开始 -->
	<div id="selectUserRoleDiv" style="display:none;padding:10px">
		<table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>
	</div>
	<!-- 分配角色弹出层结束 -->
	
	
	<script src="/resources/layui/layui.js"></script>
	<script type="text/javascript">
	    var tableIns;
	    layui.extend({
			dtree:'/resources/layui_ext/dist/dtree'
		}).use([ 'jquery', 'layer', 'form', 'table','dtree','laydate'  ], function() {
			var $ = layui.jquery;
			var layer = layui.layer;
			var form = layui.form;
			var table = layui.table;
			var dtree=layui.dtree;
			var laydate=layui.laydate;
			
			laydate.render({
				elem:'#hiredate',
				type:'datetime'
			})
			
			//渲染数据表格
			 tableIns=table.render({
				 elem: '#userTable'   //渲染的目标对象
			    ,url:'/user/loadAllUser' //数据接口
			    ,title: '用户数据表'//数据导出来的标题
			    ,toolbar:"#userToolBar"   //表格的工具条
			    ,height:'full-200'
			    ,cellMinWidth:100 //设置列的最小默认宽度
			    ,page: true  //是否启用分页
			    ,cols: [ [   //列表数据
			       {field:'id', title:'ID',align:'center',width:'80'}
			      ,{field:'name', title:'用户姓名',align:'center',width:'100'}
			      ,{field:'loginname', title:'登陆名称',align:'center',width:'120'}
			      ,{field:'address', title:'用户地址',align:'center',width:'180'}
			      ,{field:'remark', title:'用户备注',align:'center',width:'180'}
			      ,{field:'deptname', title:'所在部门',align:'center',width:'150'}
			      ,{field:'sex', title:'性别',align:'center',width:'100',templet:function(d){
			    	  return d.sex=='1'?'<font color=blue>男</font>':'<font color=red>女</font>';
			      }}
			      ,{field:'hiredate', title:'入职时间',align:'center',width:'180'}
			      ,{field:'leadername', title:'直属领导',align:'center',width:'150'}
			      ,{field:'imgpath', title:'用户头像',align:'center',width:'150'}
			      ,{field:'available', title:'是否可用',align:'center',width:'100',templet:function(d){
			    	  return d.available=='1'?'<font color=blue>可用</font>':'<font color=red>不可用</font>';
			      }}
			      ,{field:'ordernum', title:'排序码',align:'center',width:'120'}
			      ,{fixed: 'right', title:'操作', toolbar: '#userBar', width:280,align:'center'}
			    ] ]
			})
			//模糊查询
			$("#doSearch").click(function(){
				var params=$("#searchFrm").serialize();
				tableIns.reload({
					url:"/user/loadAllUser?"+params
				})
			});
			
			//监听头部工具栏事件
			table.on("toolbar(userTable)",function(obj){
				 switch(obj.event){
				    case 'add':
				      openAddUser();
				    break;
				  };
			})
			//监听行工具事件
		   table.on('tool(userTable)', function(obj){
			   var data = obj.data; //获得当前行数据
			   var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
			  if(layEvent === 'del'){ //删除
				  //先判断当前用户管理有没有子节点
						  layer.confirm('真的删除【'+data.title+'】这个用户管理吗', function(index){
						       //向服务端发送删除指令
						       $.post("/user/deleteUser",{id:data.id},function(res){
						    	   layer.msg(res.msg);
						    	    //刷新数据 表格
									tableIns.reload();
						       })
						     }); 
			   } else if(layEvent === 'edit'){ //编辑
			     openUpdateUser(data);
			   }else if(layEvent==='selectUserRole'){
				   openSelectUserRole(data);
			   }
			 });
			
			var url;
			var mainIndex;
			//打开添加页面
			function openAddUser(){
				mainIndex=layer.open({
					type:1,
					title:'添加用户管理',
					content:$("#saveOrUpdateDiv"),
					area:['800px','600px'],
					success:function(index){
						//清空表单数据       
						$("#dataFrm")[0].reset();
						$("#deptSelectDiv").removeClass("layui-show");
						$("#leaderDeptSelectDiv").removeClass("layui-show");
						url="/user/addUser";
					    //给排序码赋值
					     $.get("/user/loadUserMaxOrderNumber",function(obj){
					    	$("#ordernum").val(obj.value);
					    });
					}
				});
			}
			
			//监控username失去焦点事件
			$("#username").blur(function(obj){
				var username=$(this).val();
				$.get("/user/getUserNameToPinyin",{username:username},function(obj){
					$("#loginname").val(obj.value);
				})
			})
			//打开修改页面
			function openUpdateUser(data){
				mainIndex=layer.open({
					type:1,
					title:'修改用户',
					content:$("#saveOrUpdateDiv"),
					area:['800px','500px'],
					success:function(index){
						form.val("dataFrm",data);
						url="/user/updateUser";
						 //移除打开的样式
					    $("#userSelectDiv").removeClass("layui-show");
						//反选所属部门下拉树
						var deptid=data.deptid;
					    var params = dtree.dataInit("deptTree", deptid);
					    //给下拉框的text框赋值
					    $("#pid_str").val(params.context);
					    //反选领导部门的下拉树
					    var leaderid=data.mgr;
					    $.get("/user/loadUserByUserId",{id:leaderid},function(obj){
					    	var leaderDeptid=obj.deptid;
					    	 var leaderParams = dtree.dataInit("leaderDeptTree", leaderDeptid);
					    	 $("#leaderpid_str").val(leaderParams.context);
					    	 //加载领导名称的下拉列表
					    	 selectLeaderName(leaderDeptid,leaderid);
					    });
					    
					}
				});
			}
			//保存
			form.on("submit(doSubmit)",function(obj){
				//序列化表单数据
				var params=$("#dataFrm").serialize();
				$.post(url,params,function(obj){
					layer.msg(obj.msg);
					//关闭弹出层
					layer.close(mainIndex)
					//刷新数据 表格
					tableIns.reload();

				})
			});
			//初始化添加和修改页面的所属部门下拉树
			var deptTree = dtree.render({
				  elem: "#deptTree",
				  dataStyle: "layuiStyle",  //使用layui风格的数据格式
			      response:{message:"msg",statusCode:0},  //修改response中返回数据的定义
		          dataFormat: "list",  //配置data的风格为list
		          url: "/dept/loadDeptManagerLeftTreeJson?spread=1",  // 使用url加载（可与data加载同时存在）
				  icon: "2",
				  accordion:true
				});
			$("#pid_div").on("click",function(){
			  $(this).toggleClass("layui-form-selected");
			  $("#deptSelectDiv").toggleClass("layui-show layui-anim layui-anim-upbit");
			});
			dtree.on("node(deptTree)", function(obj){
			  $("#pid_str").val(obj.param.context);
			  $("#pid").val(obj.param.nodeId);
			  $("#pid_div").toggleClass("layui-form-selected");
			  $("#deptSelectDiv").toggleClass("layui-show layui-anim layui-anim-upbit");
			});
			
			//初始化添加和修改页面的领导所属部门下拉树
			var leaderDeptTree = dtree.render({
				  elem: "#leaderDeptTree",
				  dataStyle: "layuiStyle",  //使用layui风格的数据格式
			      response:{message:"msg",statusCode:0},  //修改response中返回数据的定义
		          dataFormat: "list",  //配置data的风格为list
		          url: "/dept/loadDeptManagerLeftTreeJson?spread=1",  // 使用url加载（可与data加载同时存在）
				  icon: "2",
				  accordion:true
				});
			$("#leaderpid_div").on("click",function(){
			  $(this).toggleClass("layui-form-selected");
			  $("#leaderDeptSelectDiv").toggleClass("layui-show layui-anim layui-anim-upbit");
			});
			dtree.on("node(leaderDeptTree)", function(obj){
			  $("#leaderpid_str").val(obj.param.context);
			  $("#leaderpid_div").toggleClass("layui-form-selected");
			  $("#leaderDeptSelectDiv").toggleClass("layui-show layui-anim layui-anim-upbit");
			  //得到部门ID
			  var deptid=obj.param.nodeId;
			  selectLeaderName(deptid);
			});
			//选中领导部门
			function selectLeaderName(deptid,selectId){
				 //根据部门ID查询当前部门下的所有员工
				  $.get("/user/loadUserByDeptId",{deptid:deptid},function(obj){
					  var data=obj.data;
					  var html="<option value=''>请选择领导</option>";
					  for(var i=0;i<data.length;i++){
						  html+="<option value="+data[i].id+">"+data[i].name+"</option>"
					  }
					  $("#mgr").html(html);
					  
					  //选中
					  if(selectId!=undefined){
						  $("#mgr").val(selectId);
					  }
					  form.render("select");
				  });
			}
			
			//打开分配角色的页面
			function openSelectUserRole(userdata){
				mainIndex=layer.open({
					type:1,
					title:'分配【'+userdata.name+'】的角色',
					content:$("#selectUserRoleDiv"),
					area:['700px','400px'],
					btnAlign:'c',
					btn:['<div class="layui-icon layui-icon-release">确认分配</div>','<div class="layui-icon layui-icon-close">取消分配</div>'],
					yes:function(index, layero){
						var checkStatus = table.checkStatus('roleTable')
					    var data = checkStatus.data;
						var params="id="+ userdata.id;//用户ID
						for(var i=0;i<data.length;i++){
							params+="&ids="+data[i].id;
						}
						$.post("/user/saveUserRole",params,function(obj){
							layer.msg(obj.msg);
						})
					},
					success:function(index){
						initRoleData(userdata.id);
					}
				});
			}
			
			
			
			
			//加载角色列表数据
			var roleTableIns;
			function initRoleData(userid){
				roleTableIns=table.render({
					 elem: '#roleTable' //渲染的目标对象
				    ,url:'/user/initUserRoles?id='+userid //数据接口
				    ,title: '用户数据表'//数据导出来的标题
				    ,cellMinWidth:100 //设置列的最小默认宽度
				    ,page: true  //是否启用分页
				    ,cols: [ [   //列表数据
					      {type:'checkbox', align:'center'}
					      ,{field:'id', title:'ID',align:'center'}
					      ,{field:'name', title:'角色名称',align:'center'}
					      ,{field:'remark', title:'角色备注',align:'center'}
					      ,{field:'available', title:'是否可用',align:'center',templet:function(d){
					    	  return d.available=='1'?'<font color=blue>可用</font>':'<font color=red>不可用</font>';
					      }}
					    ] ]
				})
			}
			
			
			function reloadTable(id){
				tableIns.reload({
					url:"/user/loadAllUser?deptid="+id
				})
			}
			
			
		});
		


		
		

	</script>
</body>
</html>